package org.imaginedays.androidall.ui;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.imaginedays.androidall.R;
import org.imaginedays.androidall.http.async.AsyncHttpResponseHandler;
import org.imaginedays.androidall.http.async.example.AndroidAllAsyncClientWapper;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class AsyncHttpRequestACT extends Activity {

	private static final Logger LOG = Logger
			.getLogger(AsyncHttpRequestACT.class.getCanonicalName());

	private TextView textView;

	private static final int RESULT = 0;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case RESULT:
				textView.setText((CharSequence) msg.obj);
				break;

			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.async_http_request);
		textView = (TextView) findViewById(R.id.tv_result);
	}

	public void sendRequest(View view) {
		// AndroidAllAsyncClient.getMushroomManagement("蘑菇管理定律");
		AndroidAllAsyncClientWapper.get("蘑菇管理定律", null,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Message msg = new Message();
						msg.what = RESULT;
						msg.obj = content;
						mHandler.sendMessage(msg);
						LOG.log(Level.FINE, "onSuccess");
					}

					@Override
					public void onStart() {
						super.onStart();
						LOG.log(Level.FINE, "onStart");
					}

					@Override
					public void onFinish() {
						super.onFinish();
						LOG.log(Level.FINE, "onFinish");
					}

				});
	}

}
